package com.lijie.bs_recruit.controller;


import com.github.pagehelper.PageInfo;
import com.lijie.bs_recruit.Util.uploadUtil;
import com.lijie.bs_recruit.VO.ResultVO;
import com.lijie.bs_recruit.model.Jobfair;
import com.lijie.bs_recruit.service.JobfairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijie
 * @since 2021-04-07
 */
@RestController
@RequestMapping("/jobfair")
public class JobfairController {

    @Autowired
    uploadUtil uploadUtil;
    @Autowired
    JobfairService jobfairService;

    @Value("${web.jobfair-path}")
    private String path;

    @RequestMapping("/selJobFair/{pageIndex}")
    public PageInfo<Jobfair> selJobFair(@PathVariable Integer pageIndex, @RequestBody Jobfair jobfair){
        PageInfo<Jobfair> jobfairPageInfo = jobfairService.selectJobFair(pageIndex, jobfair);
        return jobfairPageInfo;
    }

    @RequestMapping("/selJobFairById")
    public ResultVO<Jobfair> selJobFairById(@RequestBody Jobfair jobfair){
        ResultVO<Jobfair> resultVO=new ResultVO<>();
        Jobfair jobfairResult = jobfairService.selJobFair(jobfair.getJobfairId());
        if(jobfairResult!=null){
            resultVO.setMessage("succ");
            resultVO.setObject(jobfairResult);
        }else { resultVO.setMessage("error");
        }
        return resultVO;
    }

    @RequestMapping("/addJobFair")
    public ResultVO<Jobfair> addJobFair(@RequestBody Jobfair jobfair){
        ResultVO<Jobfair> resultVO=new ResultVO<>();
        Boolean aBoolean = jobfairService.addJobFair(jobfair);
        if(aBoolean) resultVO.setMessage("succ");
        else{ resultVO.setMessage("error");
        }
        return resultVO;
    }

    @RequestMapping("/updateJobFair")
    public ResultVO<Jobfair> updateJobFair(@RequestBody Jobfair jobfair){
        ResultVO<Jobfair> resultVO=new ResultVO<>();
        Boolean aBoolean = jobfairService.updateJobFair(jobfair);
        if(aBoolean) resultVO.setMessage("succ");
        else{ resultVO.setMessage("error");
        }
        return resultVO;
    }

    //用户上传头像
    @RequestMapping("/headerImg")
    public String headerImg(MultipartFile file){
        String s = null;
        try {
            s = uploadUtil.filesUpload(file, path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }
}

